const Controller = require('../../core/controller/ApiController');
const { Route, HttpGet,HttpPost, Middleware, filters } = require('../../../plugin/egg-decorator-router/lib');
const { ExceptionHandler } = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/miniapp/device')
class DeviceController extends Controller {

    //获取所有设备
    @HttpGet('/get/alldevice')
    @ExceptionHandler()
    async getAllDevice() {
        this.result.data = await this.service.miniapp.device.main.getAllDevice();
        return '查询成功'
    }

    //获取设备
    @HttpGet('/get/device/:status')
    @ExceptionHandler()
    async getDevice() {
        const rule = {
            status: 'int'
        };
        this.validate(rule,this.ctx.params);
        this.result.data = await this.service.miniapp.device.main.getDevice(this.ctx.params.status);
        return '查询成功'
    }

    //添加设备
    @HttpPost('/add')
    @ExceptionHandler()
    async addDevice() {
        const rule = {
            device_number: 'int',
            device_name: 'string',
            device_pic: 'string',
            position: 'string',
            belong_number: 'int',
            belong: 'string',
            type: 'string',
            details: 'string',
            specification: 'string'

        };
        this.validate(rule,this.ctx.request.body);
        await this.service.miniapp.device.main.addDevice(this.ctx.request.body);
        return '添加成功'
    }

    //获取设备具体信息
    @HttpGet('/details/:device_id')
    @ExceptionHandler()
    async getDetails() {
        const rule = {
            device_id: 'int',
        };
        this.validate(rule,this.ctx.params);
        this.result.data = await this.service.miniapp.device.main.getDetails(this.ctx.params.device_id);
        return '查询成功'
    }

    //搜索设备
    @HttpGet('/search')
    @ExceptionHandler()
    async searchDevice() {
        const rule = {
            name: 'string'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.device.main.searchDevice(this.ctx.request.query.name);
        return '查询成功'
    }
}

module.exports = DeviceController;
